/**
 * Created by CZ on 2017/5/3.
 */
module.exports = function ($scope,$rootScope,$localStorage,RequestService,$stateParams,$filter,modelService,$modal,$state,commonConfig) {
    $rootScope.isShowFooter = false;
    //获取单个应用
    $scope.starFlag = false;  //避免星星数据获取不成功导致星星自定义指令不识别starNumber
    $scope.bannerFlag = false;  //避免获取不了appid从而无法识别banner的myId
    RequestService.Request(commonConfig.cloudApi+'cloud/app/{id}?appId='+$stateParams.id,'GET').then(function(res){
        console.log(res);
        $scope.app = res;
        $scope.starFlag = true;
        $scope.bannerFlag = true;
    },function(error){
        console.log(error);
    });
    //获取应用列表
    RequestService.Request(commonConfig.cloudApi+'cloud/app?isOpen=1&status=1&pageSize=6&pageNum=1&orderBy=id','GET').then(function(res){
        console.log(res);
        $scope.AppsList = res.list;
    },function(error){
        console.log(error);
    });


    //星星等级评分
    $scope.max = 5;
    $scope.ratingVal = 0;
    $scope.hoverVal = 0;//一般的话，ratingVal和hoverVal都写0就可以了。
    // $scope.readonly = false; 控制是否只是显示不控制
    $scope.onHover = function(val) {
        $scope.hoverVal = val;
    };
    $scope.onLeave = function() {
        $scope.hoverVal = $scope.ratingVal;
    };
    $scope.onChange = function(val) {
        $scope.ratingVal = val;
    };
    //评价控制
    $scope.comment = '';
    $scope.textNum = 140;
    //字数限制
    $scope.$watch('comment',function(newVal,oldVal){
        $scope.textNum = 140 - newVal.length;
    });

    //评论上传
    $scope.submit = function(){
        var data = {
            "id": 0,
            "appId": $scope.app.id,
            "comment":$scope.comment ,
            "star": $scope.ratingVal,
            "inputDate": $filter("date")(new Date(),'yyyy-MM-dd HH:mm:ss')
        };
        console.log(data);
        RequestService.Request(commonConfig.cloudApi+'cloud/appcomment','POST', data).then(function (res) {
            //console.log(res);
            if(res.code == 0 ){
                modelService.openTipsModel('评论成功！');
                //清空数据
                $scope.comment = '';
                $scope.ratingVal = 0;
                //刷新评论
                 getCommentList();
            }else {
                modelService.openTipsModel(res.message);
            }
        }, function (error) {
            console.log(error);
        });

        RequestService.Request(commonConfig.cloudApi+'cloud/app/updateStarNumber/'+$stateParams.id,'PUT').then(function (res) {
            console.log(res);
        }, function (error) {
            console.log(error);
        });

    };
    getCommentList();
    function getCommentList(){
        //获取评价列表
        RequestService.Request(commonConfig.cloudApi+'cloud/appcomment?appId='+$stateParams.id,'GET').then(function(res){
            console.log(res);
            $scope.CommentList = res.list;
        },function(error){
            console.log(error);
        });
    }
    
    //添加应用到租户
    $scope.buyApp = function(){
        $modal.open({
            animation: true,
            templateUrl: './views/model/ExitModel.html',
            size: '',
            controller: function($scope,$modalInstance) {
                $scope.content = '您未登录，请先进行登录';
                $scope.ok = function () {
                    $rootScope.Login();
                    $modalInstance.close();
                };
                $scope.cancel = function(){
                    $modalInstance.close();
                }
            }
        });
    };
};